(function(){var MyProfile = angular.module('MyProfile', []);

MyProfile.controller('MyProfileController', ['$scope','$http','$sce','$rootScope',function($scope,$http,$sce,$rootScope) {
	var profile = (location.search.split('='))[1];
	$scope.currentProfile = profile;
	var User;
	var rty = '567';
	var that = this;
	
	 $.post("GetUserDetailes",
			  { Username : profile }
			).then(function(response) {
				User = JSON.parse(response);
				$('#NickName').html(User.NickName);
				$('#Description').html(User.ShortDescription);
				$('#Following').html(User.Folloews);
				$('#Followers').html(User.Followers);
				$scope.ProfileImg = User.PhotoURL;
				

          	
          },
				
				    function(returnedData){
				         console.log(returnedData);
				});
	
	$scope.GoToHomePage = function() {
		window.location = '\main.html';
	}
	 
    $scope.follow = function(followeName){
  	  $.post("FollowUser",
  			  { followe :  followeName}
				).then(function(response) {
          	
					if(response == "Success")
					{
						//alert("Success");
						//$("#errorAlert").hide();
						$("#successAlert").show();
						setTimeout(function(){
							$('#successAlert').hide();
						},3000);
						          			
					}
					else
					{
					if(response == "alredyExist"){
						//alert("You are already following " + followeName);
						$("#errorAlert").show();
						setTimeout(function(){
							$('#errorAlert').hide();
						},3000);
											}
      		
					}
				},
				
				    function(returnedData){
				         console.log(returnedData);
					});
    }

    $scope.followcurrentprofile = function(){
    	  $.post("FollowUser",
    			  { followe :  profile}
  				).then(function(response) {
            	
  					if (response == "Success")
  					{
  						//alert("Success");
  						
						$("#successAlert").show();
						setTimeout(function(){
							$('#successAlert').hide();
						},3000);
						            			
  					}
  					else
  					{
  					if(response == "alredyExist"){
  						//alert("You are already following " + profile);
  						$("#errorAlert").show();
  						setTimeout(function(){
							$('#errorAlert').hide();
						},3000);
  						 					}
        		
  					}
  				},
  				
  				    function(returnedData){
  				         console.log(returnedData);
  					});
      }

    $scope.CheckSession = function(){
  	  if(!$scope.test){
  		  $.post("CheckSessionUserName",
  				  { }
  				).then(function(response) {
              	
  					if (response == "Success")
  					{
  						//alert("Welcome");
              			
  					}
  					else
  					{
//  						alert("ERROR - you need to register");
  						window.location = '\login.html';
          		
  					}
  				},
  				
  				    function(returnedData){
  				         console.log(returnedData);
  					});
  		$scope.test = 1;
  	  }
    };

}]);
MyProfile.controller('MyProfileMessagesController', ['$scope','$http','$sce','$interval',function($scope,$http,$sce,$interval) {
	var profile = (location.search.split('='))[1];
	$.post("GetUserTenLastMessaes",
			  { Username : profile }
			).then(function(response) {
				$scope.UserTenMessagesArray = JSON.parse(response);
//				$scope.TenMessagesArray = MessagesArray;
				$interval($scope.update, 10,1);
        	
        },
				
				    function(returnedData){
				         console.log(returnedData);
				});
  

  $scope.update = function(){
	      $.post("GetUserTenLastMessaes",
				  { Username : profile }
				).then(function(response) {
					var MessagesArray = JSON.parse(response);
					$scope.UserTenMessagesArray = MessagesArray;
//					$("#messageDesply").html($scope.TenMessagesArray);
//					window.setTimeout($scope.update, 10000);
					$interval($scope.update, 20000);
	            },
					
					    function(returnedData){
					         console.log(returnedData);
					});
  }
  
  $scope.getTime = function(string){
   	  var d = new Date(string);
   	  var now = new Date();
   	  var returntext;
   	  var milicesoundsdiff = (now.getTime() - d.getTime());
   	  if((milicesoundsdiff / 1000)< 60){
   		  returntext = (Math.round(milicesoundsdiff / 1000)).toString() +' seconds ago';
   		  return $sce.trustAsHtml(returntext);
   	  }else if((milicesoundsdiff / 60000) < 60){
   		  returntext = (Math.round(milicesoundsdiff / 60000)).toString() +' minutes ago'; 
   		  return $sce.trustAsHtml(returntext);
   	  }else if((milicesoundsdiff / 3600000) < 24){
			  returntext = (Math.round(milicesoundsdiff / 3600000)).toString() +' hours ago'; 
			  return $sce.trustAsHtml(returntext);
   	  }else{
   		  returntext = d.getDate().toString() + '/' +d.getMonth().toString() + '/' + d.getFullYear() + ' at' +d.getHours() + ':' +d.getMinutes();
   		  return $sce.trustAsHtml(returntext);
   	  }
   	  
   	  return d;
     }
     
     $scope.al = function (){
   	  alert('works');
     }
     
     //this function is to turn the returned messages into
     //a html.
     $scope.setcheck = function(text){
   	  
   	  return $sce.trustAsHtml(text);
     }
     
     //this method is for posting a new message
		$scope.AddSubMessage = function(text){
			//find the place of the topic in the message
			var topicStartIndex = text.indexOf('#');
			var topicEndIndex   = text.indexOf(' ',topicStartIndex);
			var topic;
		
			if(topicStartIndex != -1){  
				//highlight the topic 
				if(topicEndIndex == -1){
					match = text.substring(topicStartIndex,text.length);
				}else{
					match = text.substring(topicStartIndex,topicEndIndex);
				}
		  
				topic = match.substring(1);
			}
			$.post("PostNewMessage",
					{ Topic 	:     topic,
					Content : 	  text }
				).then(function(response) {
					var json = JSON.parse(response);
					alert(json["Autor"]);
				
         	
				},
				
				    function(returnedData){
         	
				         console.log(returnedData);
					});
			
			}
}]);
MyProfile.controller('MyProfileFollowController', ['$scope','$http','$sce','$interval',function($scope,$http,$sce,$interval) {
	var profile = (location.search.split('='))[1];
	$scope.profile = profile;
	
	$.post("ReturnUsersWhoIFollow",
			  { UseName : profile }
			).then(function(response) {
				$scope.TenUsersWhoIFollow = JSON.parse(response);
//				$scope.TenMessagesArray = MessagesArray;
				$interval($scope.update, 10,1);
      	
      },
				
				    function(returnedData){
				         console.log(returnedData);
				});


$scope.update = function(){
	      $.post("ReturnUsersWhoIFollow",
				  { UseName : profile }
				).then(function(response) {
					var MessagesArray = JSON.parse(response);
					$scope.TenUsersWhoIFollow = MessagesArray;
//					$("#messageDesply").html($scope.TenMessagesArray);
//					window.setTimeout($scope.update, 10000);
					$interval($scope.update, 20000);
	            },
					
					    function(returnedData){
					         console.log(returnedData);
					});
}
}]);

MyProfile.controller('MyProfileFollowMeController', ['$scope','$http','$sce','$interval',function($scope,$http,$sce,$interval) {
	var profile = (location.search.split('='))[1];
	$scope.profile = profile;
	
	$.post("ReturnUsersWhoFollowMe",
			  { UseName : profile }
			).then(function(response) {
				$scope.TenUsersWhoFollowMe = JSON.parse(response);
//				$scope.TenMessagesArray = MessagesArray;
				$interval($scope.update, 10,1);
      	
      },
				
				    function(returnedData){
				         console.log(returnedData);
				});


$scope.update = function(){
	      $.post("ReturnUsersWhoFollowMe",
				  { UseName : profile }
				).then(function(response) {
					var MessagesArray = JSON.parse(response);
					$scope.TenUsersWhoFollowMe = MessagesArray;
//					$("#messageDesply").html($scope.TenMessagesArray);
//					window.setTimeout($scope.update, 10000);
					$interval($scope.update, 20000);
	            },
					
					    function(returnedData){
					         console.log(returnedData);
					});
}
}]);
})();